package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.*;

/**
 * 十、气质问卷（Qizhi）共60题
 */
public class Table10 extends AbstractTable implements TableCalculate{
    String code = "";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return null;
    }

    @Override
    public String[] report(Map map) {
        return new String[0];
    }

    private static ClsAnswerResult GetQizhiFactorScore(ClsAnswerRequest answer) {
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1胆汁质，2多血质，3粘液质，4抑郁质
            // 1正确题数
            int MAX_NUM = 60;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 共一组，2,1,0,-1,-2
            // 4行，15列的数组
            int[][] intNumberA = { { 2, 6, 9, 14, 17, 21, 27, 31, 36, 38, 42, 48, 50, 54, 58 },
                    { 4, 8, 11, 16, 19, 23, 25, 29, 34, 40, 44, 46, 52, 56, 60 },
                    { 1, 7, 10, 13, 18, 22, 26, 30, 33, 39, 43, 45, 49, 55, 57 },
                    { 3, 5, 12, 15, 20, 24, 28, 32, 35, 37, 41, 47, 51, 53, 59 } };

            String[] vAppraiseA = new String[4];
            String[] vDoctorIdeaA = new String[4];
            for (i = 0; i < vAppraiseA.length; i++) {
                vAppraiseA[i] = "";
            }

            for (i = 0; i < vDoctorIdeaA.length; i++) {
                vDoctorIdeaA[i] = "";
            }

            int[] vFactorA = new int[4]; // 实际为4个因子
            for (i = 0; i < vFactorA.length; i++) {
                vFactorA[i] = 0;
            }

            // 原始分
            for (i = 0; i < intNumberA.length; i++) {
                for (j = 0; j < intNumberA[1].length; j++) {
                    vFactorA[i] += 3 - Integer.valueOf(sAnswer[Integer.valueOf(intNumberA[i][j] - 1)]);
                }
            }

            // 将原始分从大到小排序
            float[] intValueA = new float[vFactorA.length];
            int[] intListA = { 0, 1, 2, 3 };
            String[] strGeneA = { "胆汁质", "多血质", "粘液质", "抑郁质" };
            // Arrays.Copy(vFactorA, intValueA, vFactorA.GetUpperBound(0) + 1)
            // System.arraycopy(vFactorA, 0, intValueA, 0, vFactorA.length);

            for (i = 0; i < intValueA.length; i++) {
                intValueA[i] = vFactorA[i];
                intValueA[i] += (4 - i) * 0.01; // 2017-09-25 防止分数相同
            }

            // Array.Sort(intValueA, intListA)

            Map<Float, Integer> map = new TreeMap<Float, Integer>(new Comparator<Float>() {
                public int compare(Float obj1, Float obj2) {
                    // 降序排序
                    return obj1.compareTo(obj2);
                }
            });
            for (i = 0; i < intValueA.length; i++) {
                map.put(intValueA[i], intListA[i]);
                // System.out.println(intValueA[i] + ":" + intListA[i]);
            }

            Set<Float> keySet = map.keySet();
            Iterator<Float> iter = keySet.iterator();
            i = 0;
            while (iter.hasNext()) {
                Float key = iter.next();
                intValueA[i] = key;
                intListA[i] = map.get(key);
                // System.out.println(intValueA[i] + ":" + intListA[i]);
                i += 1;
            }

            // 胆汁质，多血质，粘液质，抑郁质
            int intLocal = 0;
            for (i = intValueA.length - 1; i > 0; i--) {
                if (intValueA[i] - intValueA[i - 1] > 3) {
                    intLocal = i;
                    break;
                }
            }

            for (i = 3; i >= intLocal; i--) {
                // 诊断结果
                vAppraiseA[intListA[i]] = strGeneA[intListA[i]];
                // 按原始分得到的指导意见
                vDoctorIdeaA[intListA[i]] = "Qizhi." + (intListA[i] + 1) + ".1";
            }

            ClsScore.Qizhi qizhi = new ClsScore.Qizhi();
            qizhi.setDzz_o(vFactorA[0]);
            qizhi.setDzz_a(vAppraiseA[0]);
            qizhi.setDzz_d(vDoctorIdeaA[0]);

            qizhi.setDxz_o(vFactorA[1]);
            qizhi.setDxz_a(vAppraiseA[1]);
            qizhi.setDxz_d(vDoctorIdeaA[1]);

            qizhi.setNyz_o(vFactorA[2]);
            qizhi.setNyz_a(vAppraiseA[2]);
            qizhi.setNyz_d(vDoctorIdeaA[2]);

            qizhi.setYyz_o(vFactorA[3]);
            qizhi.setYyz_a(vAppraiseA[3]);
            qizhi.setYyz_d(vDoctorIdeaA[3]);

            result.setObject(qizhi);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }

}
